package com.module.antiFake.manager;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;

import com.module.antiFake.dao.AntiFakeOrgConfigDao;
import com.module.antiFake.vo.AntiFakeOrgConfig;

public final class AntiFakeOrgConfigManager extends GenericHibernateManager<AntiFakeOrgConfig, Long>{

	private static AntiFakeOrgConfigManager service;
	private AntiFakeOrgConfigDao dao = AntiFakeOrgConfigDao.getInstance();
	private AntiFakeOrgConfigManager(){

	}

	public synchronized static AntiFakeOrgConfigManager getInstance(){
		if(service==null){
			service = new AntiFakeOrgConfigManager();
		}
		return service;
	}

	/**
	 * 保存或者更新企业扫码预警设置
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public boolean saveOrUpdateConfig(AntiFakeOrgConfig antiFakeOrgConfig) throws Exception{ 
		boolean flag=false;
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
//		AntiFakeOrgConfig antiFakeOrgConfig = bean.getAntiFakeOrgConfig();
		try{
			AntiFakeOrgConfig form = dao.findOrgConfig(session,antiFakeOrgConfig.getOrgid());
			if(form!=null){
				form.setFirstCodeTimes(antiFakeOrgConfig.getFirstCodeTimes());
				form.setSecondCodeTimes(antiFakeOrgConfig.getSecondCodeTimes());
				form.setTranTimes(antiFakeOrgConfig.getTranTimes());
				flag = dao.update(session,form);
			}else{
				flag = dao.save(session,antiFakeOrgConfig);
			}
			tx.commit();
		}catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}		
		return flag;
	}
	/**
	 * 查询
	 * @param bean
	 * @throws Exception
	 */
	public AntiFakeOrgConfig findOrgConfig(String orgid) throws Exception{
		Session session = this.getSession();
		try{
			AntiFakeOrgConfig form = dao.findOrgConfig(session,orgid);
//			bean.setAntiFakeOrgConfig(form);
			return form;
		}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}	
	}

	@Override
	public GenericHibernateDao<AntiFakeOrgConfig, Long> getDao() {
		return dao;
	}
}
